package com.zyw.weibo.ui;

import com.zyw.weibo.data.local.SharedPreferencesManager;
import com.zyw.weibo.data.remote.CodeException;
import com.zyw.weibo.data.remote.NetOperatingManager;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.emagsoftware.content.pm.PackageMgr;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.UIThread;
import cn.emagsoftware.ui.dialog.DialogManager;
import cn.emagsoftware.util.LogManager;

public class Login extends Base {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(!Welcome.IS_APP_RUNNING){
			Intent intent = PackageMgr.getLaunchIntentForPackage(this, getPackageName());
			startActivity(intent);
			finish();
			return;
		}
		setContentView(R.layout.login);
		Button title = (Button)findViewById(R.id.btnTopbarCenter);
		title.setText(R.string.login_title);
		final EditText username = (EditText)findViewById(R.id.edtLoginUsername);
		final EditText password = (EditText)findViewById(R.id.edtLoginPassword);
		Button login = (Button)findViewById(R.id.btnLoginLogin);
		login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String usernamestr = username.getText().toString();
				final String passwordStr = password.getText().toString();
				if(TextUtils.isEmpty(usernamestr) || TextUtils.isEmpty(passwordStr)){
					ToastManager.showLong(Login.this, R.string.login_login_empty);
					return;
				}
				new UIThread(Login.this){
					private ProgressDialog pDialog = null;
					protected void onBeginUI(Context context) {
						pDialog = DialogManager.showProgressDialog(context, R.string.generic_dialog_title, R.string.login_login_process, null, null, true, false);
						pDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
							@Override
							public void onDismiss(DialogInterface dialog) {
								// TODO Auto-generated method stub
								cancel();
							}
						});
					};
					protected Object onRunNoUI(Context context) throws Exception {
						String result = NetOperatingManager.login(context, usernamestr, passwordStr);
						if(result != null) return result;
						NetOperatingManager.refreshCurUser(context);
						NetOperatingManager.refreshCurGroups(context);
						return result;
					};
					protected void onSuccessUI(Context context, Object result) {
						pDialog.dismiss();
						String resultStr = (String)result;
						if(resultStr == null){
							SharedPreferencesManager.setUsernameAndPassword(context, usernamestr, passwordStr);
							Intent intent = new Intent(Login.this, Main.class);
							startActivity(intent);
							Login.this.finish();
						}else{
							DialogManager.showAlertDialog(context, context.getString(R.string.generic_dialog_title), resultStr, new String[]{
								context.getString(R.string.generic_dialog_btn_ok)
							}, null, true, false);
						}
					};
					protected void onExceptionUI(Context context, Exception e) {
						pDialog.dismiss();
						LogManager.logE(Login.class, "login failed.", e);
						String errorInfo = null;
						if(e instanceof CodeException) errorInfo = e.getMessage();
						else errorInfo = context.getString(R.string.generic_error);
						DialogManager.showAlertDialog(context, context.getString(R.string.generic_dialog_title), errorInfo, new String[]{
							context.getString(R.string.generic_dialog_btn_ok)
						}, null, true, false);
					};
				}.start();
			}
		});
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_BACK:
    		exit();
    		return true;
    	}
    	return super.onKeyDown(keyCode, event);
    }
	
}
